package memory.net;

import java.util.HashMap;


import memory.Tools;

public class ObjectDB {
	
	private static HashMap<Integer, Boolean> usedIDs = new HashMap<Integer, Boolean>();
	
	private HashMap<Integer, Object> objects = new HashMap<Integer, Object>();
	
	public static int getID() {
		while(true) {
			Integer id = Tools.getRandomNr(65500);
			if(!usedIDs.containsKey(id)) {
				usedIDs.put(id, true);
				return id;
			}
		}
	}
	
	public int add(Integer id, Object o) {
		if(!objects.containsKey(id)) {
			objects.put(id, o);
			return id;
		}
		return -1;
		
		/*while(true) {
			Integer id = Tools.getRandomNr(65500);
			if(!objects.containsKey(id)) {
				objects.put(id, o);
				return id;
			}
		}*/
	}
	
	public Object get(Integer id) {
		if(objects.containsKey(id)) 
			return objects.get(id);
		else
			return null;
		
	}
	
}
